/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package smartgrid;

import org.orm.PersistentException;
public class DataLineProcessor {
	private int ID;
	
	private java.util.Date timeStamp;
	
	private double Value;
	
	private String Unit;
	
	private short Hora;
	
	private short Minuto;
	
	private String action="";
	
	public void setID(int value) {
		this.ID = value;
	}
	
	public int getID() {
		return ID;
	}
	
	public void setTimeStamp(java.util.Date value) {
		this.timeStamp = value;
	}
	
	public java.util.Date getTimeStamp() {
		return timeStamp;
	}
	
	public void setValue(double value) {
		this.Value = value;
	}
	
	public double getValue() {
		return Value;
	}
	
	public void setUnit(String value) {
		this.Unit = value;
	}
	
	public String getUnit() {
		return Unit == null ? "" : Unit;
	}
	
	public void setHora(short value) {
		this.Hora = value;
	}
	
	public short getHora() {
		return Hora;
	}
	
	public void setMinuto(short value) {
		this.Minuto = value;
	}
	
	public short getMinuto() {
		return Minuto;
	}
	
	public String getAction() {
		return action;
	}
	
	public void setAction(String action) {
		this.action = action;
	}
	
	private int contract_contractID;
	
	public void setContract_contractID(int value) {
		this.contract_contractID = value;
	}
	
	public int getContract_contractID() {
		return contract_contractID;
	}
	
	private int tariff_tariffID;
	
	public void setTariff_tariffID(int value) {
		this.tariff_tariffID = value;
	}
	
	public int getTariff_tariffID() {
		return tariff_tariffID;
	}
	
	public String process() {
		String result = "Unexcepted result";
		if (action.equals("search")) {
			smartgrid.DataLine _dataLine = smartgrid.DataLine.loadDataLineByORMID(getID());
			if (_dataLine!= null) {
				copyFromBean(_dataLine);
				result = "Search success";
			}
			else {
				result = "Search failed";
			}
		}
		else if(action.equals("insert"))  {
			try {
				smartgrid.DataLine _dataLine = smartgrid.DataLine.createDataLine();
				copyToBean(_dataLine);
				if (_dataLine.save()) {
					result = "Insert success";
				}
				else {
					result = "Insert failed";
				}
			}
			catch (Exception e) {
				result = "Insert error: " + e.toString();
			}
		}
		else if (action.equals("update")) {
			smartgrid.DataLine _dataLine= smartgrid.DataLine.loadDataLineByORMID(getID());
			if (_dataLine != null) {
				copyToBean(_dataLine);
				if (_dataLine.save()) {
					result = "Update success";
				}
				 else {
					result = "Update failed";
				}
			}
			 else  {
				result = "Update failed";
			}
			
		}
		else if (action.equals("delete")) {
			smartgrid.DataLine _dataLine = smartgrid.DataLine.loadDataLineByORMID(getID());
			if (_dataLine != null && _dataLine.deleteAndDissociate()) {
				result = "Delete success";
			}
			else {
				result = "Delete failed";
			}
		}
		else if (action.equals("")) {
			result = "";
		}
		action = "";
		return result;
	}
	
	private void copyFromBean(smartgrid.DataLine _dataLine) {
		setTimeStamp(_dataLine.getTimeStamp());
		setValue(_dataLine.getValue());
		setUnit(_dataLine.getUnit());
		setHora(_dataLine.getHora());
		setMinuto(_dataLine.getMinuto());
		setID(_dataLine.getORMID());
		
		{
			smartgrid.Contract _contract = _dataLine.getContract();
			if (_contract != null) {
				setContract_contractID(_contract.getORMID());
			}
		}
		
		
		{
			smartgrid.Tariff _tariff = _dataLine.getTariff();
			if (_tariff != null) {
				setTariff_tariffID(_tariff.getORMID());
			}
		}
		
	}
	
	private void copyToBean(smartgrid.DataLine _dataLine) {
		_dataLine.setTimeStamp(getTimeStamp());
		_dataLine.setValue(getValue());
		_dataLine.setUnit(getUnit());
		_dataLine.setHora(getHora());
		_dataLine.setMinuto(getMinuto());
		smartgrid.Contract _contract = smartgrid.Contract.loadContractByORMID(getContract_contractID());
		_dataLine.setContract(_contract);
		smartgrid.Tariff _tariff = smartgrid.Tariff.loadTariffByORMID(getTariff_tariffID());
		_dataLine.setTariff(_tariff);
	}
	
}

